//升级优化 by 啤酒冲米二 && k1
// 15.07.15--22.2.17
// Farooq's Revive 1.5 (原始版本)

/////////////////////////////////////下面变量可以修改////////////////////////////////////
//_unit setVariable ["FAR_SIDE_Medic", true, true]; //设置变量让指定的AI能救治玩家

//_unit getVariable ["FAR_count_down", 0] //玩家倒下计数
//_unit getVariable ["FAR_count_revive", 0] //玩家救人计数
//_unit getVariable ["FAR_count_kill", 0] //玩家击倒计数
//_unit getVariable ["FAR_isUnconscious", 0] //单位倒下的变量，刚倒下会有3秒左右的延迟
//_unit getVariable ["FAR_once_eh", true] //单位倒下的相关变量，无延迟可以立即判断单位是否倒下
//_unit getVariable ["FAR_unit_Side", side _unit] //单位倒下会变平民阵营，此变量保存了原先的阵营

//FAR_disable_Friend = 1; //默认nil未定义，显示倒下标记等需要双方友好度大于等于0.6
//player setVariable ["FAR_downMak_en", false] //让设置此变量的玩家不显示其他玩家倒地标记
FAR_MuteACRE = false; // true:倒下无法使用ACRE无线电,不能收听或使用近距离聊天
if(isNil "FAR_EnableDeathMessages")then {FAR_EnableDeathMessages = true}; //使用倒下和击伤提示
/*
	0 = 只有医疗兵才能治疗无需医疗包和急救包
	1 = 任何人都能救治无需医疗包和急救包
	2 = 必须有医疗包有黄包的只能止血
	3 = 玩家只要有医疗包(不消耗)或急救箱(-1)就可以治疗
	4 = 任何人都能救治有医疗包的能满血救活，没有的可选择消耗黄包来满血救治
*/
if(isNil "FAR_ReviveMode")then {FAR_ReviveMode = 3}; //1,4模式无止血菜单, 0,2,3模式满足医治条件时隐藏止血菜单
if(isNil "FAR_actMode")then {FAR_actMode = 0}; //治疗与止血的模式 3种模式可选0~2
if(isNil "FAR_reviveTime")then {FAR_reviveTime = 10}; //复苏伤员所需时间
if(isNil "FAR_StabilizeTime")then {FAR_StabilizeTime = 5}; //为伤员止血所需时间
if(isNil "FAR_BleedOut")then {FAR_BleedOut = 600}; //流血死亡超时 0值禁用显示倒下时间,无止血菜单
if(isNil "FAR_bleedTime")then {FAR_bleedTime = true}; //false 禁止显示3d流血读秒
if(isNil "FAR_in_Spectator")then {FAR_in_Spectator = false}; //true 倒下强制进入观战模式
if(isNil "FAR_cam_mod")then {FAR_cam_mod = true}; //false 观战模式只允许玩家视角
if(isNil "FAR_cam_free")then {FAR_cam_free = true}; //false 禁用自由视角调整
if(isNil "FAR_death_Score")then {FAR_death_Score = false}; //true 启用计分板死亡计数
if(isNil "FAR_kill_Score")then {FAR_kill_Score = false}; //true 启用计分板击杀计数
if(isNil "FAR_map_mak")then {FAR_map_mak = true}; //false 地图上不显示倒地标记
if(isNil "FAR_dist")then {FAR_dist = 80}; //多少米内显示3D标记 0值不显示
if(isNil "FAR_Talk_chat")then {FAR_Talk_chat = false}; //true 禁用语音&文字交流
if(isNil "FAR_map_gps")then {FAR_map_gps = false}; //true 禁止打开地图和GPS
if(isNil "FAR_callAI_en")then {FAR_callAI_en = true}; //false 禁止呼叫AI救治
if(isNil "FAR_AI_RS")then {FAR_AI_RS = true}; //false 禁用AI互救
if(isNil "FAR_DISABLE_ATAG")then {FAR_DISABLE_ATAG = false}; //true 禁止AI呼叫的AI与敌人交战
if(isNil "FAR_callAI_dist")then {FAR_callAI_dist = 500}; //AI救治最大距离限制
if(isNil "FAR_allow_kill")then {FAR_allow_kill = false}; //是否允许杀死倒地的敌人 DISABLE
if(isNil "FAR_allow_creep")then {FAR_allow_creep = true}; //false 禁止爬行
if(isNil "FAR_water_rs")then {FAR_water_rs = true}; //false 不允许水下救人和止血
if(isNil "FAR_Remove_body")then {FAR_Remove_body = false}; //true 删除玩家尸体
if(isNil "FAR_killFriend")then {FAR_killFriend = false}; //true 开启击伤队友惩罚
if(isNil "FAR_allow_killFriend")then {FAR_allow_killFriend = 2}; //允许短期误伤队友次数
if(isNil "FAR_killFriend_time")then {FAR_killFriend_time = 180}; //惩罚时间
if(isNil "FAR_death_Effect")then {FAR_death_Effect = true}; //false 关闭死亡特效
if(isNil "FAR_playableUnit")then {FAR_playableUnit = false}; //true 多人模式玩家小队AI(可扮演的AI)启用FAR
if(isNil "FAR_SIDE_Medic")then {FAR_SIDE_Medic = []}; //除了玩家阵营还允许哪些阵营的AI救治玩家
if(isNil "FAR_join_side")then {FAR_join_side = false}; //true 非同阵营的AI救治玩家时短暂加入玩家阵营
if(isNil "FAR_Invincible")then {FAR_Invincible = true}; //false 倒下单位可以被杀死
if(isNil "FAR_shot_creep")then {FAR_shot_creep = false}; //true 爬动会被敌人攻击
if(isNil "FAR_bleed_creep2")then {FAR_bleed_creep2 = [20,83]}; //爬动被攻击流血 随机20秒 + 随机[最小值,最大值]
if(isNil "FAR_un_script")then {FAR_un_script = {}}; //单位倒下时执行脚本代码
if(isNil "FAR_revive_script")then {FAR_revive_script = {}}; //单位成功救治伤员时执行脚本代码
if(isNil "FAR_KnockDown_script")then {FAR_KnockDown_script = {}}; //玩家击倒单位时执行脚本代码
if(isNil "FAR_callAI_condition")then {FAR_callAI_condition = {true}}; //呼叫AI救治所需条件
if(isNil "FAR_callAI_checkCond")then {FAR_callAI_checkCond = {false}}; //AI救治过程条件true取消救治
if(isNil "FAR_allow_suicide")then {FAR_allow_suicide = true}; //false 禁用自杀
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FAR_isDragging = false;
FAR_isCarry = false;
FAR_deathMessage2 = [];
FAR_Damage_make = [];
FAR_inveh_EH = [];
FAR_remote_EH = [];
FAR_ADD_EH_HD = [];
FAR__units = [];
FAR_emptyPos_veh = [];
FAR_Key_sw = false;
FAR_creep_injured = "xxx";
FAR1stGunner = false;
FAR_coerce_Spectator = true;
FAR_suicide = 0;
FAR_crewTime = 3; //4
FAR_killFriend_number = 0;
FAR_killFriend_Check = true;
FAR_Respawn_debug = true; //重生初始化除错
if(isNil "FAR_Enable")then{FAR_Enable = true}; //false: 禁用FAR
if(isNil "FAR_ThrowSmokeProbability")then{FAR_ThrowSmokeProbability = 40};

call compile preprocessFileLineNumbers "FAR_revive2\FAR_revive_funcs.sqf"; /////

if (isDedicated) exitWith {
	{_x addPublicVariableEventHandler FAR_public_EH} forEach ["FAR_Damage_make","FAR_inveh_EH","FAR_ADD_EH_HD"];
	"FAR_remote_EH" addPublicVariableEventHandler FAR_remote_exec;
	call FAR_fn_isDedicated;
};

call compile preprocessFileLineNumbers "FAR_revive2\FAR_Spectating.sqf"; /////

#define SCRIPT_VERSION "1.611 plus"

FAR_Mute_ACRE = {
	waitUntil {time > 0};
	waitUntil {
		if (alive player) then [{
			if ((player getVariable["ace_sys_wounds_uncon", false])) then {
				private["_saveVolume"];
				_saveVolume = acre_sys_core_globalVolume;
				player setVariable ["acre_sys_core_isDisabled", true, true];
				waitUntil {
					acre_sys_core_globalVolume = 0;
					if (!(player getVariable["acre_sys_core_isDisabled", false])) then {
						player setVariable ["acre_sys_core_isDisabled", true, true];
						[true] call acre_api_fnc_setSpectator;
					};
					!(player getVariable["ace_sys_wounds_uncon", false])
				};
				if ((player getVariable["acre_sys_core_isDisabled", false])) then {
					player setVariable ["acre_sys_core_isDisabled", false, true];
					[false] call acre_api_fnc_setSpectator;
				};
				acre_sys_core_globalVolume = _saveVolume;
			};
		},{ waitUntil {alive player} }];
		sleep .25;
		false
	};
};

FAR_fn_killFriend = {
	_check = "";
	_time = time + 300;
	waitUntil {_check = (time > _time || {!FAR_killFriend}); (_check || {FAR_killFriend_number > FAR_allow_killFriend})};
	if (_check) exitWith {FAR_killFriend_number = 0; FAR_killFriend_Check = true;};
	waitUntil {alive player};
	player call FAR_out_veh;
	player setVariable ["FAR_is_killFriend", true, true]; 
	if (surfaceIsWater getPosASL player) then {player setPos [0,0,999]; sleep 2;};
	player switchMove "amovpercmstpsraswrfldnon";
	sleep .5;
	format ["%1 连续误伤队友%2次被送去远处看风景%3秒.", name player,FAR_killFriend_number,FAR_killFriend_time] remoteExec ["systemChat", 0, false];
	_pos = getPosASL player;
	_po2 = [random 50,random 50,20];
	_out = time + FAR_killFriend_time;
	waitUntil {
		waitUntil {alive player};
		if((getPosASL player) distance [0,0,0] > 80)then{player setPos _po2};
		if(!userInputDisabled)then{disableUserInput true};
		if(simulationEnabled player)then{player enableSimulation false};
		_plyOut = round (_out - time);
		[format["<t size = '.8'>你需要在这里冷静冷静！<br/>惩罚剩余时间: %1秒</t>", _plyOut],-1,0,3,0,0,7374] spawn BIS_fnc_dynamicText;
		sleep 1;
		(time > _out || {!FAR_killFriend})
	};
	disableUserInput false;
	player setVelocity [0,0,0];
	player enableSimulation true;
	player setPosASL _pos;
	FAR_killFriend_number = 0;
	FAR_killFriend_Check = true;
	player setVariable ["FAR_is_killFriend", false, true]; 
	['<t size=".8">请注意敌我识别不要再误伤队友！</t>',-1,.01 * safeZoneH + safeZoneY,15,0,0,7374] spawn BIS_fnc_dynamicText;
}; //队友误伤惩罚

///////////////////////////////////////////初始化///////////////////////////////////////////////
waitUntil {!isNull player}; //
0 spawn {
	{_x addPublicVariableEventHandler FAR_public_EH} forEach ["FAR_deathMessage2","FAR_Damage_make","FAR_inveh_EH","FAR_ADD_EH_HD"];
	"FAR_remote_EH" addPublicVariableEventHandler FAR_remote_exec;
	
	if(player getVariable ["FAR_isUnconscious", 2] == 1)then{[player,true] remoteExec ["allowDamage",0]; player playMove "amovppnemstpsraswrfldnon"};//
	
	[player] call FAR_Respawn_Init;
	
	player setVariable ["FAR_count_kill", 0, true];
	player setVariable ["FAR_count_down", 0, true];
	player setVariable ["FAR_count_revive", 0, true];
	player setVariable ["FAR_is_killFriend", false, true];
	
	//if (FAR_Respawn_debug) then {player setVariable ["FAR_Respawn", true, true]}; //重生debug
	
	if (player getVariable ["FAR_Respawn", true]) then {
		player addMPEventHandler ["MPRespawn", {_this spawn FAR_Respawn_Init}];
		player setVariable ["FAR_Respawn", false, true];
	}; //重生事件
	
	if(isMultiplayer && {FAR_Respawn_debug})then{0 spawn FAR_fn_RespawnDebug}; ////
	
	if (isServer) then [{uiSleep 28},{uiSleep 20}]; //
	if (!FAR_Enable) exitWith {
		if (FAR_MuteACRE) then {0 spawn FAR_Mute_ACRE};
		hintSilent format["Farooq's Revive %1 已禁用.", SCRIPT_VERSION];
		systemchat "FAR医疗已禁用";
	};
	if (FAR_MuteACRE) then [{
		0 spawn FAR_Mute_ACRE;
		hintSilent format["Farooq's Revive %1 初始化完成.%2", SCRIPT_VERSION, "\n\n 注意:无意识的单位将无法使用电台,听到别人或使用近距离聊天"];
	},{
		hintSilent format["Farooq's Revive %1 初始化完成.", SCRIPT_VERSION];
		systemchat format["FAR revive %1 k1升级版", SCRIPT_VERSION];
	}];
	_str = switch FAR_ReviveMode do {
			case 0: {"0: 只有医疗兵才能治疗无需医疗包和急救包"};
			case 1: {"1: 任何人都能救治无需医疗包和急救包"};
			case 2: {"2: 必须有医疗包有黄包的只能止血"};
			case 3: {"3: 玩家只要有医疗包(不消耗)或急救箱(-1)就可以治疗"};
			case 4: {"4: 任何人都能救治有医疗包的能满血救活，其他可选择消耗黄包来满血救治"};
		default {"?"};
	};
	systemchat format["医疗模式%1", _str];
};

if (isMultiplayer) then [{
    if (isNil "FAR_spectating_type") then {FAR_spectating_type = "ALL"}; //"Friendly" "SIDE" "group"
	if !(player getVariable ["FAR_downMak_en", true]) exitWith {};
    {
		if (_x getVariable ["FAR_isUnconscious", 8] == 1 && {alive _x}) then {
			if(vehicle _x != _x)then{_x playMoveNow "ainjppnemstpsnonwrfldnon_rolltoback"}; //在载具的动作
			
			_unit = _x;
			if (({_unit in _x} count FAR__units) > 0) exitWith {};
			_makeName = Format["FAR_%1_%2",_x,time/60];
			if (FAR_map_mak) then {
				_unitSide = _x getVariable ["FAR_unit_Side", side _x];
				_plySide = player getVariable ["FAR_unit_Side", side player];
				if (((_unitSide getFriend _plySide >= .6) && {_plySide getFriend _unitSide >= .6}) || {!isNil "FAR_disable_Friend"}) then {
					_make = createMarkerLocal [_makeName, getPosASL _x];
					_makeName setMarkerTypeLocal "hd_dot";
					_makeName setMarkerTextLocal (name _x) + " 倒下";
					_makeName setMarkerColorLocal "ColorRed";
				};
			};
			FAR__units pushback [_x, _makeName]; //
		};
	} forEach (call k1_allUnits); //刚加入游戏时为无意识单位创建标记等 playableUnits
},{FAR_spectating_type = "group"}];

//////////////////////////////////////////////主循环///////////////////////////////////////////////////
uiSleep 1;
0 spawn {
	private ["_play", "_reviveTime", "_StabilizeTime", "_idx1", "_idx2", "_idx3", "_idx4", "_idx5", "_idx6", "_idx7", "_idx21", "_idx22", "_idx23", "_idx24", "_idx25", "_idx26", "_idx27", "_index", "_a", "_b", "_time1", "_time2"];
	_a = -1;
	_time1 = 0;
	_time2 = 0;
	_idx1 = -1;
	_idx21 = -1;
	waitUntil {
		if (time > _time1) then {
			if (player getVariable ["FAR_isUnconscious", 2] == 2 && {FAR_Enable} && {alive player} && {player isKindOf "CaManBase"} && {!(["UAV_AI", typeOf player] call BIS_fnc_inString)}) then { [player] spawn FAR_Respawn_Init }; //
			
			if (player getVariable ["FAR_isUnconscious", 0] == 1) then {
				if (player == vehicle player) then {
					FAR_dir_save = getDir player;
					if (!FAR_coerce_Spectator) then {FAR_coerce_Spectator = true};
				};
				
				if (_a == -1 && {!FAR_Key_sw}) then {
					_a = ppEffectCreate ["colorCorrections", 8100];
					_a ppEffectAdjust [1,1.02,-.02,[1,1,1,0],[1,1,1,0],[.75,.25,0,1]]; //[1, 1.6, -.35,]
					_a ppEffectCommit 0;
					_a ppEffectEnable true;
					_b = ppEffectCreate ["filmGrain", 8120];
					_b ppEffectAdjust [.3, 2, 4, .5, .5, true];
					_b ppEffectCommit 0;
					_b ppEffectEnable true;
					FAR_creep_injured = if(FAR_allow_creep)then[{"amovppnemsprslowwrfldf_injured"},{"xxx"}];
				}; //黑白屏效果
				
				if (inputAction "action" > 0 && {FAR_allow_suicide} && {alive player}) then [{
					if (FAR_suicide > 6) then [{
						FAR_suicide = 0;
						cutText ["", "Plain Down"]; //TitleText
						player setCaptive false;
						if(player == vehicle player)then{player switchMove 'deadstate'};
						player setDamage 1;
						if(!isNil 'xxx_CamShake')then{resetCamShake; xxx_CamShake = nil};
					},{
						_str = switch FAR_suicide do {
							case 0: {"------"};
							case 1: {">-----"};
							case 2: {">>----"};
							case 3: {">>>---"};
							case 4: {">>>>--"};
							case 5: {">>>>>-"};
							case 6: {">>>>>>"};
							default {">>>>>>"};
						};
						cutText [format["%1\n你确定要这么做？", _str], "Plain Down"];
					}];
					FAR_suicide = FAR_suicide + 1;
				},{if(FAR_suicide > 0)then{FAR_suicide = 0; cutText ["", "Plain Down"]}}];
				
				_uav = getConnectedUAV player;
				_uavControl = (UAVControl _uav) select 1;
				if (_uavControl != "") then {player action ["SwitchToUAVDriver", _uav]; _uav action ["BackFromUAV", player]}; //禁用UAV
				
				if (!isNull(FindDisplay 602)) then {closedialog 0}; //
			};
			
			if (_a != -1 && {player getVariable ["FAR_isUnconscious", 0] == 0 || {FAR_Key_sw}}) then {
				ppEffectDestroy _a;
				ppEffectDestroy _b;
				FAR_creep_injured = "xxx";
				_a = -1; //
			};

			//添加滚轮菜单
			if (FAR_actMode in [0,1] && {_idx1 == -1}) then {
				_idx1 = [player, Format["<t size='1' color='#C90000'>%1</t>", localize "STR_far_kill_tag"], "a3\ui_f\data\IGUI\Cfg\Revive\overlayIconsGroup\f100_ca.paa", "a3\ui_f\data\IGUI\Cfg\Revive\overlayIconsGroup\f100_ca.paa", "call FAR_Check_killAct", "call FAR_Check_killAct",{}, {}, {FAR_target setDamage 1}, {}, [], 2, 96, false, false] call BIS_fnc_holdActionAdd;
				_idx2 = player addAction [Format["<t size='1' color='#C90000'>%1</t> <img size='1' color='#ffffff' shadow='0' image='a3\ui_f\data\map\MapControl\hospital_ca.paa'/>", localize "STR_far_heal_FAK"], {FAR_ReviveMode4_sw = 1; if(FAR_actMode == 0)then[{FAR_target spawn FAR_HandleRevive},{if(FAR_actMode == 1)then{FAR_target spawn FAR_HandleRevive2}}]}, [], 96, true, true, "", "call FAR_Check_Revive2"];
				_idx3 = player addAction [Format["<t size='1' color='#C90000'>%1</t> <img size='1' color='#ffffff' shadow='0' image='a3\ui_f\data\map\MapControl\hospital_ca.paa'/>", localize "STR_far_heal"], {if(FAR_actMode == 0)then[{FAR_target spawn FAR_HandleRevive},{if(FAR_actMode == 1)then{FAR_target spawn FAR_HandleRevive2}}]}, [], 96, true, true, "", "call FAR_Check_Revive"];
				_idx4 = player addAction [Format["<t size='1' color='#C90000'>%1</t> <img size='1.3' color='#ffffff' shadow='1' image='a3\ui_f\data\IGUI\Cfg\Cursors\unitBleeding_ca.paa'/>", localize "STR_far_hemostasis"], {if(FAR_actMode == 0)then[{FAR_target spawn FAR_HandleStabilize},{if(FAR_actMode == 1)then{FAR_target spawn FAR_HandleStabilize2}}]}, [], 96, true, true, "", "call FAR_Check_Stabilize"];
				_idx5 = player addAction [Format["<t color='#C90000'>%1</t>", localize "STR_far_drag"], {FAR_target spawn FAR_Drag}, [], 95, false, true, "", "call FAR_Check_Dragging"];
				_idx6 = player addAction [Format["<t color='#C90000'>%1</t>", localize "STR_far_Carry"], {FAR_target spawn FAR_Carry}, [], 95, false, true, "", "call FAR_Check_Carry"];
				_idx7 = [player, Format["<t color='#C90000'>%1</t>", localize "STR_far_Remove_wounded"], "a3\ui_f\data\IGUI\Cfg\Actions\getout_ca.paa", "a3\ui_f\data\IGUI\Cfg\Actions\getout_ca.paa", "call FAR_Check_MoveOut", "call FAR_Check_MoveOut",{}, {}, {_unit = FAR_veh_crew select 0; FAR_remote_EH = [_unit, nil, "outveh", nil]; publicVariable "FAR_remote_EH"; _unit spawn FAR_out_veh;}, {}, [], FAR_crewTime, 95, false, false, false] call BIS_fnc_holdActionAdd;
			
				_index = player addEventHandler ["Killed", { if(FAR_Remove_body)then {(_this select 0) spawn {waitUntil {alive player}; deleteVehicle _this}} }]; //删除玩家尸体
				_play = player;
			};
			if (_idx1 != -1 && {_play != player || {!(FAR_actMode in [0,1])}}) then {
				{_play removeAction _x} forEach [_idx1, _idx2, _idx3, _idx4, _idx5, _idx6, _idx7];
				_play removeEventHandler ["Killed", _index];
				_idx1 = -1;
				FAR_Key_sw = false; //
			};
			
			if (FAR_actMode == 2 && {_idx21 == -1}) then {
				_idx21 = [player, Format["<t size='1' color='#C90000'>%1</t>", localize "STR_far_kill_tag"], "a3\ui_f\data\IGUI\Cfg\Revive\overlayIconsGroup\f100_ca.paa", "a3\ui_f\data\IGUI\Cfg\Revive\overlayIconsGroup\f100_ca.paa", "call FAR_Check_killAct", "call FAR_Check_killAct",{}, {}, {FAR_target setDamage 1}, {}, [], 2, 96, false, false] call BIS_fnc_holdActionAdd;
				_idx22 = [player, Format["<t size='1' color='#C90000'>%1</t>", localize "STR_far_heal_FAK"], "\A3\Ui_f\data\IGUI\Cfg\Revive\overlayIcons\u100_ca.paa", "\A3\Ui_f\data\IGUI\Cfg\Revive\overlayIcons\r100_ca.paa", "call FAR_Check_Revive2", "call FAR_Check_Revive2", FAR_rs_mod3Start, {}, {FAR_ReviveMode4_sw = 1; FAR_target spawn FAR_HandleRevive3; FAR_target setVariable ["FAR_call_AI", objNull, true]}, FAR_rs_mod3Close, [], FAR_reviveTime, 96, false, false] call BIS_fnc_holdActionAdd;
				_idx23 = [player, Format["<t size='1' color='#C90000'>%1</t>", localize "STR_far_heal"], "\A3\Ui_f\data\IGUI\Cfg\Revive\overlayIcons\u100_ca.paa", "\A3\Ui_f\data\IGUI\Cfg\Revive\overlayIcons\r100_ca.paa", "call FAR_Check_Revive", "call FAR_Check_Revive", FAR_rs_mod3Start, {}, {FAR_target spawn FAR_HandleRevive3; FAR_target setVariable ["FAR_call_AI", objNull, true]}, FAR_rs_mod3Close, [], FAR_reviveTime, 96, false, false] call BIS_fnc_holdActionAdd;
				_idx24 = [player, Format["<t color='#C90000'>%1</t>", localize "STR_far_hemostasis"], "a3\ui_f\data\IGUI\Cfg\Cursors\empty64_ca.paa", "a3\ui_f\data\IGUI\Cfg\Cursors\empty64_ca.paa", "call FAR_Check_Stabilize", "call FAR_Check_Stabilize", FAR_rs_mod3Start, {}, {FAR_target spawn FAR_HandleStabilize3; FAR_target setVariable ["FAR_call_AI", objNull, true]}, FAR_rs_mod3Close, [], FAR_StabilizeTime, 96, false, false] call BIS_fnc_holdActionAdd;
				_idx25 = player addAction [Format["<t color='#C90000'>%1</t>", localize "STR_far_drag"], {FAR_target spawn FAR_Drag}, [], 95, false, true, "", "call FAR_Check_Dragging"];
				_idx26 = player addAction [Format["<t color='#C90000'>%1</t>", localize "STR_far_Carry"], {FAR_target spawn FAR_Carry}, [], 95, false, true, "", "call FAR_Check_Carry"];
				_idx27 = [player, Format["<t color='#C90000'>%1</t>", localize "STR_far_Remove_wounded"], "a3\ui_f\data\IGUI\Cfg\Actions\getout_ca.paa", "a3\ui_f\data\IGUI\Cfg\Actions\getout_ca.paa", "call FAR_Check_MoveOut", "call FAR_Check_MoveOut",{}, {}, {_unit = FAR_veh_crew select 0; FAR_remote_EH = [_unit, nil, "outveh", nil]; publicVariable "FAR_remote_EH"; _unit spawn FAR_out_veh;}, {}, [], FAR_crewTime, 95, false, false, false] call BIS_fnc_holdActionAdd;
				
				_index = player addEventHandler ["Killed", { if(FAR_Remove_body)then {(_this select 0) spawn {waitUntil {alive player}; deleteVehicle _this}} }];
				_play = player;
				_reviveTime = FAR_reviveTime;
				_StabilizeTime = FAR_StabilizeTime;
			};
			if (_idx21 != -1 && {_play != player || {FAR_actMode != 2} || {FAR_reviveTime != _reviveTime} || {FAR_StabilizeTime != _StabilizeTime}}) then {
				{_play removeAction _x} forEach [_idx21, _idx22, _idx23, _idx24, _idx25, _idx26, _idx27];
				_play removeEventHandler ["Killed", _index];
				_idx21 = -1;
				FAR_Key_sw = false;
			};
			
			{
				_x params ["_unit", "_makeName"];
				
				if (local _unit && {isMultiplayer} && {!isPlayer _unit} && {!FAR_playableUnit}) then {
					_unit call FAR_var_init;
					_unit remoteExec ["FAR_fn_AISET2", 0];
					if(vehicle _unit == _unit)then{_unit switchMove 'deadstate'};
					_unit setDamage 1;
				}; //多人模式倒地AI除错 如果AI变量未重置考虑用playableUnits
				
				if (_unit getVariable ["FAR_isUnconscious", 0] == 1 && {alive _unit}) then [{
					if(getMarkerColor _makeName != "")then{_makeName setMarkerPosLocal (getPosASL _unit)};
				},{
					if(getMarkerColor _makeName != "")then{deleteMarkerLocal _makeName};
					FAR__units = FAR__units - [_x]; ////
					//FAR__units set [_forEachIndex, "EMPTY"]; //*!
					//FAR__units = FAR__units - ["EMPTY"];
				}];
			} forEach FAR__units; //删除单位标记
		
			{
				_unit = _x select 0;
				if (local _unit) then {
					_unit call FAR_veh_eh;
					_unit call FAR_coerce_Move;
				};
				if (!isMultiplayer) then {
					(_unit getVariable ["FAR_AI_group", [false, "", 0]]) params ["_var", "_grp", "_id"];
					if (_var && {vehicle _unit == _unit} && {alive _unit} && {_unit getVariable "FAR_isUnconscious" == 1}) then {
						_unit joinAsSilent [_grp, _id]; //
						_unit setVariable ["FAR_AI_group", nil];
					};
				};
			} forEach FAR__units; //强制动作&加入原来小队
			
			{(_x select 0) call FAR_fn_breath} forEach FAR__units;
			
			if (FAR_AI_RS) then {{(_x select 0) call FAR_fn_AI_RS} forEach FAR__units};
			
			_time1 = time + 1;
		};
		
		if (FAR_playableUnit && {time > _time2}) then {
			{(_x select 0) call FAR_fn_AISET1} forEach FAR__units;
			_time2 = time + 5;
		}; //AI相关设置
		
		false
	};
};

///////////////////////////////////////////3D标记/////////////////////////////////////////
FAR_Draw3D = addMissionEventHandler ["EachFrame", { //Draw3D: ui失去焦点会停止！
	if (player getVariable ["FAR_downMak_en", true]) then {
		{
			_unit = _x select 0;
			if (_unit distance player < FAR_dist && {_unit getVariable ["FAR_isUnconscious", 8] == 1} && {alive _unit} && {(vehicle _unit != vehicle player) || {cameraView == "External"}}) then {
				_unitSide = _unit getVariable ["FAR_unit_Side", side _unit];
				_plySide = player getVariable ["FAR_unit_Side", side player];
				if (((_unitSide getFriend _plySide >= .6) && {_plySide getFriend _unitSide >= .6}) || {!isNil "FAR_disable_Friend"}) then {
					_r = (_unit getVariable ["FAR_reviveTime", [0,0]]) select 1;
					_s = (_unit getVariable ["FAR_StabilizeTime", [0,0]]) select 1;
					_str1 = if(_r > 0)then[{format[" r:%1％", _r]},{""}];
					_str2 = if(_s > 0)then[{format[" s:%1％", _s]},{""}];
					drawIcon3D ["a3\ui_f\data\map\MapControl\hospital_ca.paa", [1,0,0,1], _unit modelToWorldVisual [0,0,0], .5, .5, 0, format["%1 (%2%3m%4%5)", name _unit, _unit getVariable ["FAR_unitbleedOut", ""], ceil(player distance _unit), _str1, _str2], 0, .025, "PuristaMedium", "center", true]; //.02
				}; //pos Object?
			};
		} forEach FAR__units;
	};
	
	if (getOxygenRemaining player < .1 && {player isKindOf "CaManBase"} && {!(["UAV_AI", typeOf player] call BIS_fnc_inString)} && {player getVariable ["FAR_once_eh", true]} && {alive player} && {FAR_Enable}) then {
		player setOxygenRemaining 1;
		_PartDamage = [player getHit "hands", player getHit "legs"];
		[player, objNull, objNull, _PartDamage] spawn FAR_unit_Unconscious;
	};
	
	if (player getVariable ["FAR_isUnconscious", 0] == 1) then {
		if (inputAction "moveForward" > 0 && {FAR_allow_creep} && {isNull objectParent player} && {getPosASL player select 2 > -.15} && {!(player getVariable ["FAR_isDragged",false])} && {!(player getVariable ["FAR_isCarry",false])} && {alive player}) then [{
			player playMoveNow "amovppnemsprslowwrfldf_injured";
		},{
			if(animationState player == "amovppnemsprslowwrfldf_injured")then{player playMoveNow "amovppnemstpsraswrfldnon_injured"};
		}]; //W爬行
		
		if (FAR_map_gps && {visibleMap}) then {openMap [false,false]};
	};
}]; //removeMissionEventHandler ["Draw3D", FAR_Draw3D];

//////////////////////////////////////////AI使用FAR/////////////////////////////////////////
if (isMultiplayer && {!FAR_playableUnit}) exitWith {};
if (isMultiplayer) then {uiSleep 5}; //

{
	if (_x != player && {!(["UAV_AI", typeOf _x] call BIS_fnc_inString)}) then {
		_x removeAllEventHandlers "HandleDamage"; //
		_x addEventHandler ["HandleDamage", {if (FAR_playableUnit || {!isMultiplayer}) then {_this call FAR_HandleDamage_EH}}];
	};
	
	if (!isPlayer _x && {local _x} && {_x getVariable ["FAR_Respawn", true]} && {alive _x} && {!(["UAV_AI", typeOf _x] call BIS_fnc_inString)}) then {
		_x call FAR_var_init;
		if (isMultiplayer) then {_x addMPEventHandler ["MPRespawn", {_this spawn FAR_Respawn_Init}]; _x setVariable ["FAR_Respawn", false, true]; _x setVariable ["FAR_before_group", group _x, true]}; ////
	};
} forEach (if(isMultiplayer)then[{playableUnits},{units group player}]);


//////////////////////////////////////////////////////////////////////////////////
if (isNil "CBA_Settings_fnc_init" || {!isNil "FAR_ADDCBA_SET"}) exitWith {};
FAR_ADDCBA_SET = 1;
[
    "FAR_KEY_DEBUG",
    "CHECKBOX",
   ["修复按键功能", "执行后请改回默认值，不然下次开始任务会自动执行此除错操作"],
    "FAR医疗设置",
    false,
    false,
    { if (FAR_KEY_DEBUG) then {if (!isNil {FAR_KeyDown}) then {(findDisplay 46) displayRemoveEventHandler ["KeyDown", FAR_KeyDown]; (findDisplay 46) displayRemoveEventHandler ["MouseMoving", FAR_MMH]; FAR_KeyDown = nil}; if (player getVariable ['FAR_isUnconscious',0] == 1) then {call FAR_addKey}; systemChat "已执行"} }
] call CBA_Settings_fnc_init;


